package com.loongtech.bi.manager.system;

import com.loongtech.bi.entity.POJO.Page;
import com.loongtech.bi.entity.system.EntityConfig;
import com.loongtech.bi.support.Log;
import com.loongtech.core.jpa.manager.ManagerBase;
import com.loongtech.core.util.Utils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.loongtech.bi.manager.system.SysProjectManager.projectIdNameMap;

@Service("configManager")
public class ConfigManager extends ManagerBase<EntityConfig> {
	private static final long serialVersionUID = 5134272747892532556L;

    public static final Map<String, String> CONFIG = new HashMap<>();  // 所有配置项
    public static double RADIO = 100;  // 汇率

    public static final String HAS_ITEM_DETAIL_REASON = "HAS_ITEM_DETAIL_REASON";  // 是否有物品获得/消耗细分原因
    public static final String impala_jdbc_url = "impala_jdbc_url";  // impala逻辑库
    public static final String user_hive_database = "user_hive_database";  // impala逻辑库地址
    public static final String impala_jdbc_driver = "org.apache.hive.jdbc.HiveDriver";  // hive驱动
    public static final String HAS_DETAIL_REASON = "HAS_DETAIL_REASON";  // 货币流水中是否有细分原因
    public static final String MONEY_TYPE = "MONEY_TYPE";  // 游戏充值相关的货币类型
    public static final String GAME_NAME = "GAME_NAME";  // 游戏名称
    public static final String GAME_RMB_RADIO = "GAME_RMB_RADIO";  // 游戏汇率
    public static final String GAME_RECHARGE_FLOW_NAME = "GAME_RECHARGE_FLOW_NAME";  // 充值流水名称标识
    public static final String ALL_ACTIVATE_URL = "ALL_ACTIVATE_URL";  // 激活设备总数url
    public static final String PLATFORM_CHANNEL_URL = "PLATFORM_CHANNEL_URL";  // 各平台激活设备数url
    public static final String CHANNEL_ACTIVATE_URL = "CHANNEL_ACTIVATE_URL";  // 各渠道激活设备数url
    public static final String PROJECT_NAME = "PROJECT_NAME";  // 本工程名
    public static final String SELF_URL = "SELF_URL";  // 本工程信息(bi_root)的url
    public static final String PROJECT_URL = "PROJECT_URL";  // 工程信息(bi_root)的url
    public static final String SERVER_CLASS = "SERVER_CLASS";  // 服务器是否分类，分的话首页可切换各种类型服务器
    public static final String ONLINE_RADIO = "ONLINE_RADIO";  // 在线日志频率，默认是5分钟
    public static final String S_257 = "S_257 ";  // 对帐功能中addtype为257的要特殊处理，257算游戏内充值
    public static final String REPORT_EMAIL_ADDR = "REPORT_EMAIL_ADDR";  // 每日监控报表邮件地址
    public static final String ALARM_HOST = "ALARM_HOST";  // 告警调用接口地址
    public static final String STOP_MONITOR = "STOP_MONITOR";  // 禁止启动监控
    public static final String SERVER_TYPE = "SERVER_TYPE";  // 服务器类型 mid, backend
    public static final String OPEN_DATE = "OPEN_DATE";

    public void init() {
        List<EntityConfig> all = findAll();
        CONFIG.clear();
        for (EntityConfig e : all) {
            CONFIG.put(e.getEnumName(), e.getEnumValue());

            if (GAME_RMB_RADIO.equals(e.getEnumName())) {
                try {
                    RADIO = Double.parseDouble(e.getEnumValue());
                } catch (Exception ex) {
                    RADIO = 100;
                }

            }
        }
    }

	/**
	 * 根据枚举名称获得配置值
	 * @param enumName
	 * @return
	 */
	public String getConfigValueByName(String enumName){
		if(null == enumName || enumName.trim().isEmpty()){
			Log.bi.error("ConfigManager getConfigValueByName enumName is null !");
			return null;
		}

        if (CONFIG.get(enumName) != null) {
            return CONFIG.get(enumName);
        } else {
            EntityConfig value = getBy(EntityConfig.K.enumName,enumName);
            if (value != null) {
                return value.getEnumValue();
            } else {
                return null;
            }
        }
	}

    /**
     * 查询任务(springmvc)
     */
    public Page<List<EntityConfig>> queryByCriteria(Integer curPage, Integer pageSize, EntityConfig e) {
        List<Object> param = new ArrayList<>();
        StringBuilder hql = new StringBuilder(" from EntityConfig e where 1=1 ");
        if (!Utils.isEmptyStr(e.getDescription())) {
            hql.append(" and e.description like ?");
            param.add("%" + e.getDescription() + "%");
        }
        if (e.getGameId() != null) {
            hql.append(" and e.gameId like ?");
            param.add("%" + e.getGameId() + "%");
        }
        if (!Utils.isEmptyStr(e.getEnumName())) {
            hql.append(" and e.enumName like ?");
            param.add("%" + e.getEnumName() + "%");
        }
        if (!Utils.isEmptyStr(e.getEnumValue())) {
            hql.append(" and e.enumValue like ?");
            param.add("%" + e.getEnumValue() + "%");
        }
        hql.append(" order by id desc");
        Integer totalSize = countNum(hql.toString(), param.toArray());
        //数据
        List<EntityConfig> list = findByQuery((curPage - 1) * pageSize, pageSize, hql.toString(), param.toArray());
        if(list != null && list.size() > 0) {
            for (EntityConfig entityConfig : list) {
                String gameid = entityConfig.getGameId();
                for (Map.Entry<String, String> entry : projectIdNameMap.entrySet()) {
                    gameid = gameid.replace(entry.getKey() + ",", entry.getValue() + ",");
                }
                entityConfig.setGameName(gameid);
            }
        }
        return new Page<>(list, totalSize, curPage, pageSize);
    }
}
